﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using System.Windows.Forms;

namespace ProgNetComponentsDx.Language.Design.Editors
{
    public class RadioItemValueEditor : ProgNetComponents.Design.Editors.BaseListEditor
    {
        protected override bool AlwaysRefreshItems
        {
            get
            {
                return true;
            }
        }
        protected override bool FillCollection(ProgNetComponents.Collections.NamedObjectCollection collection, System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            object own = null;
            if (context.Instance is RadioItemLngSettings)
            {
                own = (context.Instance as RadioItemLngSettings).Owner;
            }
            else if (context.Instance != null)
            {
                own = context.Instance.GetFieldValue("Owner");
                if (own == null)
                {
                    own = context.Instance.GetPropertyValue("Owner");
                }
            }

            RadioGroupItemCollection col = null;
            if (own is RadioGroup)
            {
                col = (own as RadioGroup).Properties.Items;
            }

            if (own is RepositoryItemRadioGroup)
            {
                col = (own as RepositoryItemRadioGroup).Items;
            }
            if (col != null)
            {
                string str = "";
                collection.Clear();
                foreach (RadioGroupItem item in col)
                {
                    str = item.Value.AsString();
                    if (collection.Contains(str))
                    {
                        if (item.Value != null)
                            str = item.Value.GetType().Name + "[" + str + "]";
                    }
                    collection.Add(str, item.Value);
                }
            }
            return true;
        }
    }
}
